/**
 * 图片管理的单例
 */
var OttImage = {
    id: "ottImageTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
OttImage.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '名称', field: 'show_name', align: 'center', valign: 'middle', sortable: true},
        {title: '分辨率', field: 'resolution', align: 'center', valign: 'middle', sortable: true},
        {title: '原图大小', field: 'size', align: 'center', valign: 'middle', sortable: true},
        {title: '原图',field: 'file_name', align: 'center',
            formatter: function(value,row,index){
            	 return '<a href="'+value+'" download >'
                 +'<img  src="'+value+'"  width="150px" class="img-rounded" >'
                 +'</a>';
            }
        },
        {title: '压缩后大小', field: 'compress_size', align: 'center', valign: 'middle', sortable: true},
        {title: '压缩图',field: 'compress_name', align: 'center',
            formatter: function(value,row,index){
                return '<a href="'+value+'" download >'
                +'<img  src="'+value+'"  width="75px" class="img-rounded" >'
                +'</a>';
            }
        }]
    return columns;
};


/**
 * 检查是否选中
 */
OttImage.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        OttImage.seItem = selected[0];
        return true;
    }
};


/**
 * 点击修改按钮时
 */
OttImage.openChangeImage = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '编辑图片，点击图片下载',
            area: ['800px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/image/image_edit/' + this.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除图片
 */
OttImage.delImage = function () {
    if (this.check()) {

        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/image/remove", function () {
                Feng.success("删除成功!");
                OttImage.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ottImageId", OttImage.seItem.id);
            ajax.start();
        };

        Feng.confirm("是否删除图片 " + OttImage.seItem.name + "?",operation);
    }
};
OttImage.clippingImage = function () {
    if (this.check()) {
    	 test(OttImage.seItem.id);
    }
};

/**
 * 搜索图片
 */
OttImage.search = function () {
    var queryData = {};
    queryData['showName'] = $("#showName").val();
    OttImage.table.refresh({query: queryData});
}

$(function () {
    var defaultColunms = OttImage.initColumn();
    var table = new BSTable(OttImage.id, "/image/list", defaultColunms);
    table.setPaginationType("client");
    table.init();
    OttImage.table = table;
});
